<?php

Raise::load('core.RaiseObject');
Raise::load('core.collection.RaiseCollection');
Raise::load('core.helper.config.RaiseSettings');
Raise::load('core.io.file.RaisePath');

/**
 * RaiseMime class
 * retrieves MIME and file extensions based on the mime.ini register
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.io
 * @since 1.2
 */
class RaiseLocale extends RaiseObject {
    
    /**
     * The country code
     * @var string
     */
    private $country;
    
    /**
     * The language code
     * @var string
     */
    private $language;
    
    /**
     * Create a RaiseLocale with its locale information
     * @param string $language The ISO 639-1 2 letter lower case language code
     * @param string $country The ISO 3166-1 2 letter upper case country code
     */
    public function __construct($language, $country){
       $this->language = strtolower($language);
       $this->country = strtoupper($country);
    }
    
    /**
     * Get the ISO 639-1 2 letter lower case language code of this locale
     * @return string
     */
    public function language(){
        return $this->language;
    }
    
    /**
     * Get the ISO 3166-1 2 letter upper case country code of this locale
     * @return string
     */
    public function country(){
        return $this->country;
    }
    
    public function __toString(){
        return $this->language . '-' . $this->country;
    }
    
    /**
     * Collection of language entries
     * @var RaiseCollection
     * @static
     */
    private static $languages;
    
    /**
     * Collection of country entries
     * @var RaiseCollection
     * @static
     */
    private static $countries;

    /**
     * Load the MIME types from the file
     * @static
     */
    private static function loadLanguages(){
        if(!(self::$languages instanceof RaiseCollection)){
            $settings = new RaiseSettings(RaisePath::combine(RaisePath::phpRaisePath(), 'locale.ini'));
            self::$languages = $settings->get('language');
            if(!self::$languages){
                self::$languages = new RaiseCollection();
            }
        }
    }

    /**
     * Load the MIME types from the file
     * @static
     */
    private static function loadCountries(){
        if(!(self::$countries instanceof RaiseCollection)){
            $settings = new RaiseSettings(RaisePath::combine(RaisePath::phpRaisePath(), 'locale.ini'));
            self::$countries = $settings->get('country');
            if(!self::$countries){
                self::$countries = new RaiseCollection();
            }
        }
    }

    /**
     * Get the country name based on the country code
     * @param string $code
     * @return string
     * @static
     */
    public static function countryName($code){
        self::loadCountries();
        $a = self::$countries->get($code);
        return $a;
    }

    /**
     * Get the country name based on the country code
     * @param string $name
     * @return string
     * @static
     */
    public static function countryCode($name){
        self::loadCountries();
        $k = self::$countries->indexOf($name);
        return $k;
    }

    /**
     * Get the language name based on the language code
     * @param string $code
     * @return string
     * @static
     */
    public static function languageName($code){
        self::loadLanguages();
        $a = self::$languages->get($code);
        return $a;
    }

    /**
     * Get the language name based on the language code
     * @param string $name
     * @return string
     * @static
     */
    public static function languageCode($name){
        self::loadLanguages();
        $k = self::$languages->indexOf($name);
        return $k;
    }

}

